﻿package mortal.game.view.mount
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.mount.panel.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.palyer.view.*;
    import mortal.game.view.playerSystem.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class MountController extends PlayerBaseController
    {
        private var _mountModule:MountModule;
        private var _mountProxy:MountProxy;
        private var _mountCache:MountCache;
        private var _state:int;

        public function MountController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._mountProxy = GameProxy.mount;
            this._mountCache = Cache.instance.mount;
            return;
        }// end function

        override protected function initView() : GSprite
        {
            if (this._mountModule == null)
            {
                this._mountModule = new MountModule(this.window);
            }
            return this._mountModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.MountRide, this.rideMount);
            Dispatcher.addEventListener(EventName.MountCancelAutoLevelup, this.cancelAntoLevelUp);
            Dispatcher.addEventListener(EventName.MountEquip, this.mountEquip);
            Dispatcher.addEventListener(EventName.BestShowMountWin, this.openWin);
            Dispatcher.addEventListener(EventName.MountPopupWin, this.popupWinHandler);
            Dispatcher.addEventListener(EventName.MountUseSkillBook, this.mountUseSkillBook);
            return;
        }// end function

        override public function addWinShowEvent() : void
        {
            this.updateMountAvtivityWin();
            NetDispatcher.addCmdListener(ServerCommand.MountLevelUp, this.mountLevelUpdata);
            NetDispatcher.addCmdListener(ServerCommand.MountBlessUpdata, this.mountBlessUpdata);
            NetDispatcher.addCmdListener(ServerCommand.UpdateMountEquip, this.updataEquip);
            NetDispatcher.addCmdListener(ServerCommand.MountAttrUpdate, this.updateAttr);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateMountItem);
            NetDispatcher.addCmdListener(ServerCommand.MountActivityWinUpdate, this.updateMountAvtivityWin);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.updateVip);
            NetDispatcher.addCmdListener(ServerCommand.MountSharpSuccess, this.updateMountSelect);
            NetDispatcher.addCmdListener(ServerCommand.SkillAdd, this.onMountSkillUpdate);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpgrade, this.onMountSkillUpdate);
            NetDispatcher.addCmdListener(ServerCommand.RebateDayChargeInfoBack, this.updateMountAvtivityWin);
            NetDispatcher.addCmdListener(ServerCommand.MountShapeCardStat, this.shapeCardStatUpdate);
            Dispatcher.addEventListener(EventName.MountLevelUp, this.mountLevelUp);
            Dispatcher.addEventListener(EventName.MountCardActive, this.mountCardActive);
            Dispatcher.addEventListener(EventName.MountManureGet, this.getManureCard);
            Dispatcher.addEventListener(EventName.MountManureCardOpen, this.openCard);
            Dispatcher.addEventListener(EventName.MountCardStartManure, this.startManure);
            Dispatcher.addEventListener(EventName.MountAutoLevelup, this.autoLevelUp);
            Dispatcher.addEventListener(EventName.MountSharp, this.mountSharp);
            Dispatcher.addEventListener(EventName.MountUseGrouthItem, this.mountUseGrouthItem);
            Dispatcher.addEventListener(EventName.MountUseTalentItem, this.mountUseTalentItem);
            Dispatcher.addEventListener(EventName.MountChangeSharp, this.mountChangeSharp);
            return;
        }// end function

        override public function removeWinShowEvent() : void
        {
            if (ChargeActivityWin.isViewShow)
            {
                ChargeActivityWin.instance.hide();
            }
            NetDispatcher.removeCmdListener(ServerCommand.MountLevelUp, this.mountLevelUpdata);
            NetDispatcher.removeCmdListener(ServerCommand.MountBlessUpdata, this.mountBlessUpdata);
            NetDispatcher.removeCmdListener(ServerCommand.UpdateMountEquip, this.updataEquip);
            NetDispatcher.removeCmdListener(ServerCommand.MountAttrUpdate, this.updateAttr);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateMountItem);
            NetDispatcher.removeCmdListener(ServerCommand.MountActivityWinUpdate, this.updateMountAvtivityWin);
            NetDispatcher.removeCmdListener(ServerCommand.VipLevelUp, this.updateVip);
            NetDispatcher.removeCmdListener(ServerCommand.MountSharpSuccess, this.updateMountSelect);
            NetDispatcher.removeCmdListener(ServerCommand.SkillAdd, this.onMountSkillUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.SkillUpgrade, this.onMountSkillUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.RebateDayChargeInfoBack, this.updateMountAvtivityWin);
            NetDispatcher.removeCmdListener(ServerCommand.MountShapeCardStat, this.shapeCardStatUpdate);
            Dispatcher.removeEventListener(EventName.MountLevelUp, this.mountLevelUp);
            Dispatcher.removeEventListener(EventName.MountCardActive, this.mountCardActive);
            Dispatcher.removeEventListener(EventName.MountManureGet, this.getManureCard);
            Dispatcher.removeEventListener(EventName.MountManureCardOpen, this.openCard);
            Dispatcher.removeEventListener(EventName.MountCardStartManure, this.startManure);
            Dispatcher.removeEventListener(EventName.MountAutoLevelup, this.autoLevelUp);
            Dispatcher.removeEventListener(EventName.MountSharp, this.mountSharp);
            Dispatcher.removeEventListener(EventName.MountChangeSharp, this.mountChangeSharp);
            return;
        }// end function

        override public function removeWin() : void
        {
            super.removeWin();
            if (MountAvtivityWin.isViewShow())
            {
                MountAvtivityWin.instance.hide();
            }
            return;
        }// end function

        private function popupWinHandler(param1) : void
        {
            if (this.isViewShow)
            {
                GameController.playerSystem.view.hide();
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PlayerModuleShowIndexByType, PlayerConst.TabType_Mount));
            }
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            GameManager.instance.popupModuleType(ModuleType.Mounts);
            if (this.isViewShow)
            {
                this._mountModule.showBtnFilter();
            }
            return;
        }// end function

        private function rideMount(event:DataEvent) : void
        {
            if (event.data is int)
            {
                if (Cache.instance.role.entityInfo.mountCode > 0 && int(event.data) > 0)
                {
                    return;
                }
                if (Cache.instance.role.entityInfo.mountCode == 0 && int(event.data) == 0)
                {
                    return;
                }
                this._state = int(event.data);
            }
            else
            {
                this._state = MountUtil.isRideUp ? (EMountState._EMountStateUnRide) : (EMountState._EMountStateRide);
            }
            if (this._state == EMountState._EMountStateRide)
            {
                if (!Global.isDebugModle)
                {
                    if (RolePlayer.instance.entityInfo.isInPVPFighting())
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35447));
                        return;
                    }
                }
                if (!Game.sceneInfo.limitInfo.canSingleMount())
                {
                    MsgManager.showRollTipsMsg(Language.getString(35448));
                    return;
                }
                if (Cache.instance.role.roleEntityInfo.reserveJs.maincity_flag)
                {
                    MsgManager.showRollTipsMsg(Language.getString(35449));
                    return;
                }
                if (RolePlayer.instance.isJumping)
                {
                    MsgManager.showRollTipsMsg(Language.getString(35450));
                    return;
                }
                if (CopyUtil.isInAllEscortTask)
                {
                    MsgManager.showRollTipsMsg(Language.getString(35451));
                    return;
                }
                if (Cache.instance.role.roleEntityInfo.isInCollect)
                {
                    MsgManager.showRollTipsMsg(Language.getString(35452));
                    return;
                }
                if (RolePlayer.instance.isInSwinArea)
                {
                    MsgManager.showRollTipsMsg(Language.getString(35453));
                    return;
                }
            }
            SceenOperation.interruptDoubleRest(this.callRideMount);
            return;
        }// end function

        private function callRideMount() : void
        {
            GameManager.instance.updateLastOperTime();
            this._mountProxy.setMountState(this._state);
            return;
        }// end function

        private function mountLevelUp(event:DataEvent) : void
        {
            var _loc_2:* = event.data.autoBuy as Boolean;
            var _loc_3:* = event.data.dic;
            var _loc_4:* = event.data.isAutoLevelUp as Boolean;
            this._mountProxy.advance(_loc_2, _loc_3, _loc_4);
            return;
        }// end function

        private function autoLevelUp(event:DataEvent) : void
        {
            this._mountModule.autoLevelUpHandler();
            return;
        }// end function

        private function mountLevelUpdata(param1:Object) : void
        {
            this._mountModule.updateMountInfo();
            this.updateMountAvtivityWin();
            return;
        }// end function

        private function mountBlessUpdata(param1:Object) : void
        {
            this._mountModule.blessUpdate();
            return;
        }// end function

        private function mountEquip(event:DataEvent) : void
        {
            var _loc_2:* = event.data.putonitemData as ItemData;
            var _loc_3:* = event.data.getoffItemData as ItemData;
            if (_loc_2 && _loc_2.itemInfo.level > this._mountCache.mountData.mountLevel)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35454, _loc_2.itemInfo.level), MsgRollTipsType.msgRollTips3);
            }
            this._mountProxy.dress(_loc_2 ? (_loc_2.serverData.uid) : (""), _loc_3 ? (_loc_3.serverData.uid) : (""));
            return;
        }// end function

        private function cancelAntoLevelUp(event:DataEvent) : void
        {
            if (this._mountModule)
            {
                this._mountModule.cancelAutoLevelUp();
            }
            return;
        }// end function

        private function updataEquip(param1:Object) : void
        {
            if (this._mountModule)
            {
                this._mountModule.updateEquip();
            }
            return;
        }// end function

        private function updateMountSharpItem() : void
        {
            if (this._mountModule)
            {
                this._mountModule.updateMountSharpItem();
            }
            return;
        }// end function

        private function mountCardActive(event:DataEvent) : void
        {
            this._mountProxy.activeCard(event.data as int);
            return;
        }// end function

        private function openCard(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            this._mountProxy.openCard(_loc_2);
            return;
        }// end function

        private function getManureCard(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            this._mountProxy.getCards(_loc_2);
            return;
        }// end function

        private function startManure(event:DataEvent) : void
        {
            this._mountProxy.shuffleCard();
            return;
        }// end function

        private function updateMountItem(param1:Object) : void
        {
            this._mountModule.updateMountItem();
            return;
        }// end function

        private function mountSharp(event:DataEvent) : void
        {
            if (Cache.instance.mount.mountData.mountLevel < 4)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35455), MsgRollTipsType.msgRollTips3);
            }
            if (Cache.instance.mount.mountData.sPublicMount.sharpId > 1000)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35456), MsgRollTipsType.msgRollTips3);
            }
            this._mountProxy.mountIllusion(int(event.data));
            return;
        }// end function

        private function updateAttr(param1:Object) : void
        {
            this._mountModule.updateAttrData();
            return;
        }// end function

        private function updateMountAvtivityWin(param1:Object = null) : void
        {
            var _loc_2:TMountConfig = null;
            var _loc_3:ChargeActivityData = null;
            if (Cache.instance.mount.mountData.mountLevel < 3)
            {
                if (ClockManager.instance.serverOpenDateNum <= 7 && Cache.instance.openServerSevenDay.getTargetTypeName() == Language.getString(35430) && Cache.instance.rebate.rechargeSum == 0)
                {
                    ChargeActivityWin.instance.show(window.x + window.width + 8, window.y);
                    ChargeActivityWin.instance.layer.setTop(this.window);
                    _loc_2 = MountConfig.instance.getMountInfoByCode(3);
                    _loc_3 = new ChargeActivityData();
                    _loc_3.activityType = ChargeActivityData.ActivityType_Three;
                    _loc_3.tmount = _loc_2;
                    ChargeActivityWin.instance.data = _loc_3;
                    if (MountAvtivityWin.isViewShow())
                    {
                        MountAvtivityWin.instance.hide();
                    }
                }
                else
                {
                    if (!MountAvtivityWin.isViewShow())
                    {
                        MountAvtivityWin.instance.show();
                        MountAvtivityWin.instance.show(window.x + window.width + 4, window.y + 37);
                        MountAvtivityWin.instance.layer.setTop(this.window);
                    }
                    MountAvtivityWin.instance.updateInfo();
                    if (ChargeActivityWin.isViewShow)
                    {
                        ChargeActivityWin.instance.hide();
                    }
                }
            }
            else if (Cache.instance.mount.mountData.mountLevel == 3)
            {
                if (MountAvtivityWin.isViewShow())
                {
                    MountAvtivityWin.instance.hide();
                }
                if (ClockManager.instance.serverOpenDateNum <= 7 && Cache.instance.openServerSevenDay.getTargetTypeName() == Language.getString(35430) && Cache.instance.rebate.rechargeSum < 500)
                {
                    ChargeActivityWin.instance.show(window.x + window.width + 8, window.y);
                    ChargeActivityWin.instance.layer.setTop(this.window);
                    _loc_2 = MountConfig.instance.getMountInfoByCode(4);
                    _loc_3 = new ChargeActivityData();
                    _loc_3.activityType = ChargeActivityData.ActivityType_Four;
                    _loc_3.tmount = _loc_2;
                    ChargeActivityWin.instance.data = _loc_3;
                }
            }
            if (BuyGiftItemWin.isViewShow)
            {
                BuyGiftItemWin.instance.layer.setTop(BuyGiftItemWin.instance);
            }
            return;
        }// end function

        private function updateVip(param1:Object) : void
        {
            this._mountModule.updateVip();
            return;
        }// end function

        private function updateMountSelect(param1:Object) : void
        {
            this._mountModule.updateMountSelect();
            this.updateMountSharpItem();
            return;
        }// end function

        private function onMountSkillUpdate(param1:SSkill) : void
        {
            this._mountModule.updateSkill(param1);
            return;
        }// end function

        private function mountUseSkillBook(event:DataEvent) : void
        {
            var _loc_6:TSkill = null;
            var _loc_7:TMountConfig = null;
            var _loc_8:Array = null;
            var _loc_9:Boolean = false;
            var _loc_12:SkillInfo = null;
            var _loc_14:SkillInfo = null;
            var _loc_15:SkillInfo = null;
            var _loc_2:* = event.data as ItemData;
            var _loc_3:* = SkillConfig.instance.getInfoById(_loc_2.itemInfo.effect);
            var _loc_4:* = MountConfig.instance.mountMap;
            var _loc_5:* = Cache.instance.mount.mountData.mountLevel;
            for each (_loc_7 in _loc_4)
            {
                
                if (_loc_7.skillList)
                {
                    _loc_6 = SkillConfig.instance.getInfoById(int(_loc_7.skillList));
                    if (_loc_6.posType == _loc_3.posType)
                    {
                        if (_loc_5 < _loc_7.code)
                        {
                            return MsgManager.showRollTipsMsg(Language.getStringByParam(35457, _loc_7.code), MsgRollTipsType.msgRollTips3);
                        }
                    }
                }
            }
            _loc_8 = SkillUtil.getLearnedSkillByType(ESkillCategory._EMountSkill);
            _loc_9 = _loc_8.length > 0 ? (false) : (true);
            var _loc_10:Boolean = true;
            var _loc_11:* = new Dictionary();
            var _loc_13:int = 0;
            while (_loc_13 < _loc_8.length)
            {
                
                _loc_15 = _loc_8[_loc_13];
                _loc_12 = _loc_11[_loc_15.tSkill.posType] as SkillInfo;
                if (_loc_12 == null || _loc_12 && _loc_15.tSkill.skillLevel > _loc_12.tSkill.skillLevel)
                {
                    _loc_11[_loc_15.tSkill.posType] = _loc_15;
                }
                _loc_13++;
            }
            for each (_loc_14 in _loc_11)
            {
                
                if (_loc_3.posType == _loc_14.tSkill.posType)
                {
                    if (_loc_3.skillLevel <= _loc_14.tSkill.skillLevel)
                    {
                        return MsgManager.showRollTipsMsg(Language.getString(35458), MsgRollTipsType.msgRollTips3);
                    }
                    if (_loc_3.skillLevel - _loc_14.tSkill.skillLevel > 1)
                    {
                        return MsgManager.showRollTipsMsg(Language.getString(35459), MsgRollTipsType.msgRollTips3);
                    }
                    break;
                }
            }
            GameProxy.packProxy.useItem(_loc_2.serverData.uid, _loc_2.itemInfo.code);
            return;
        }// end function

        private function mountUseGrouthItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            GameProxy.packProxy.useItem(_loc_2.uid, _loc_2.itemCode);
            return;
        }// end function

        private function mountUseTalentItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            GameProxy.packProxy.useItem(_loc_2.uid, _loc_2.itemCode);
            return;
        }// end function

        private function shapeCardStatUpdate(param1:Object) : void
        {
            this._mountModule.shapeCardStatUpdate(param1);
            return;
        }// end function

        private function mountChangeSharp(event:DataEvent) : void
        {
            this._mountProxy.changeShapeCard();
            return;
        }// end function

    }
}
